(function ($, undefined) {

    var showingSheet;

    function backButtonHandler() {
        showingSheet.close();
    }

    var kendo = window.kendo,
        support = kendo.support,
        ui = kendo.mobile.ui,
        Shim = ui.Shim,
        Widget = ui.Widget,
        OPEN = "open",
        CLOSE = "close",
        COMMAND = "command",
        BUTTONS = "a",
        CONTEXT_DATA = "customsheetContext",
        WRAP = '<div class="km-actionsheet-wrapper" />';

    var CustomSheet = Widget.extend({
        init: function (element, options) {
            var that = this,
                tablet,
                os = support.mobileOS;

            Widget.fn.init.call(that, element, options);

            options = that.options;
            element = that.element;

            element
                .addClass("km-actionsheet")
                .wrap(WRAP)
                //.on("up", BUTTONS, "_click")
                //.on("click", BUTTONS, kendo.preventDefault);
                .on("click", BUTTONS, "_click");

            that.view().bind("destroy", function () {
                that.destroy();
            });

            that.wrapper = element.parent().addClass("");

            setTimeout(function () {
                //var height = element[0].scrollHeight;
                //element.height(height);

                //
                that.shim = new Shim(that.wrapper, $.extend({
                    modal: os.ios && os.majorVersion < 7,
                    className: "km-actionsheet-root"
                }, that.options.popup));

                that._closeProxy = $.proxy(that, "_close");
                that.shim.bind("hide", that._closeProxy);
            }, 0);

            kendo.notify(that, ui);
        },

        events: [
            OPEN,
            CLOSE,
            COMMAND
        ],

        options: {
            name: "CustomSheet"
        },

        open: function (target, context) {
            var that = this;
            that.target = $(target);
            that.context = context;
            that.shim.show(target);

            //
            document.addEventListener("backbutton", backButtonHandler, false);
            showingSheet = that;
        },

        close: function () {
            this.context = this.target = null;
            this.shim.hide();

            document.removeEventListener("backbutton", backButtonHandler, false);
        },

        openFor: function (target) {
            var that = this,
                context = target.data(CONTEXT_DATA);

            that.open(target, context);
            that.trigger(OPEN, {target: target, context: context});
        },

        destroy: function () {
            Widget.fn.destroy.call(this);
            kendo.unbindResize(this._closeProxy);
            this.shim.destroy();
        },

        _click: function (e) {
            if (e.isDefaultPrevented()) {
                return;
            }

            var currentTarget = $(e.currentTarget);
            //var action = currentTarget.data("action");

            //if (action) {
            //    var actionData = {
            //            target: this.target,
            //            context: this.context
            //        },
            //        $angular = this.options.$angular;
            //
            //    if ($angular) {
            //        this.element.injector().get("$parse")(action)($angular[0])(actionData);
            //    } else {
            //        kendo.getter(action)(window)(actionData);
            //    }
            //}

            this.trigger(COMMAND, {target: this.target, context: this.context, currentTarget: currentTarget});

            e.preventDefault();
            this._close();
        },

        _close: function (e) {
            if (!this.trigger(CLOSE)) {
                this.close();
            } else {
                e.preventDefault();
            }
        }
    });

    ui.plugin(CustomSheet);
})(window.kendo.jQuery);


/* kendoMobileLink */

(function ($, undefined) {
    var kendo = window.kendo,
        mobile = kendo.mobile,
        ui = mobile.ui,
        Widget = ui.Widget,
        support = kendo.support,
        os = support.mobileOS,
        ANDROID3UP = os.android && os.flatVersion >= 300,
        CLICK = "click",
        DISABLED = "disabled",
        DISABLEDSTATE = "km-state-disabled";

    function highlightLink(widget, event, highlight) {
        $(event.target).closest("a").toggleClass("km-state-active", highlight);

        if (ANDROID3UP && widget.deactivateTimeoutID) {
            clearTimeout(widget.deactivateTimeoutID);
            widget.deactivateTimeoutID = 0;
        }
    }

    var Link = Widget.extend({
        init: function (element, options) {
            var that = this;

            Widget.fn.init.call(that, element, options);

            //
            that.element.addClass('km-link');

            //
            that.options.enable = that.options.enable && !that.element.attr(DISABLED);
            that.enable(that.options.enable);

            that._userEvents = new kendo.UserEvents(that.element, {
                allowSelection: false,
                press: function (e) {
                    if(e.event.target.tagName === 'INPUT') {
                        return;
                    }
                    that._activate(e);
                },
                release: function (e) {
                    if(e.event.target.tagName === 'INPUT') {
                        return;
                    }
                    highlightLink(that, e, false);
                }
            });

            that._userEvents.bind("tap", function (e) {
                that._release(e);
            });


            if (ANDROID3UP) {
                that.element.on("move", function (e) {
                    that._timeoutDeactivate(e);
                });
            }
        },

        destroy: function () {
            Widget.fn.destroy.call(this);
            this._userEvents.destroy();
        },

        events: [
            CLICK
        ],

        options: {
            name: "Link",
            enable: true
        },

        enable: function (enable) {
            var element = this.element;

            if (typeof enable == "undefined") {
                enable = true;
            }

            this.options.enable = enable;

            if (enable) {
                element.removeAttr(DISABLED);
            } else {
                element.attr(DISABLED, DISABLED);
            }

            element.toggleClass(DISABLEDSTATE, !enable);
        },

        _timeoutDeactivate: function (e) {
            if (!this.deactivateTimeoutID) {
                this.deactivateTimeoutID = setTimeout(highlightLink, 500, this, e, false);
            }
        },

        _activate: function (e) {
            if (this.options.enable) {
                highlightLink(this, e, true);
            }
        },

        _release: function (e) {
            var that = this;

            if(e.event.target.tagName === 'INPUT') {
                e.preventDefault();
                return;
            }

            if (e.which > 1) {
                return;
            }

            if (!that.options.enable) {
                e.preventDefault();
                return;
            }

            if (that.trigger(CLICK, {target: $(e.target), link: that.element})) {
                e.preventDefault();
            }
        }

    });

    ui.plugin(Link);
})(window.kendo.jQuery);

